﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PmSadis.Repositories.Abstract;

namespace PmSadis.WebFormsUi
{
    public partial class SendSupport : System.Web.UI.Page
    {
        /// <summary>
        /// Репозиторий для сообщений техподдержки
        /// </summary>
        private IListRepository<PmSadis.Models.Support> _repo;

        public SendSupport()
        {
            _repo = new PmSadis.Repositories.SupportRepository();
        }

        /// <summary>
        /// Добавить сообщение
        /// </summary>
        /// <param name="title">Заголовок сообщения</param>
        /// <param name="content">Содержание сообщения</param>
        /// <param name="email">Адрес отправителя</param>
        /// <param name="phone">Телефон отправителя (при наличии)</param>
        /// <returns></returns>
        private int InsertSupport(string title, string content, string email, string phone=null)
        {
            var support = new PmSadis.Models.Support
            {
                Title=title,
                Content=content,
                Email=email,
                Phone=phone
            };
            return (int)_repo.Edit(support);
        }

        protected void btnSendSupport_Click(object sender, EventArgs e)
        {
            var title = tbSupportTitle.Text;
            var content = mSupportContent.Text;
            var email = tbSupportEmail.Text;
            var phone = tbSupportPhone.Text;
            var phoneToInsert = string.IsNullOrEmpty(phone) ? null : phone;

            InsertSupport(title, content, email, phoneToInsert);
        }
    }
}